package cc.monggo.third.impl;

import java.util.HashMap;

import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import cc.monggo.third.QQService;
import cc.monggo.third.client.ThirdAuthClient;
import cc.monggo.third.model.QQAccessToken;
import cc.monggo.third.model.QQUser;

public class QQServiceImpl implements QQService {

	@Override
	public QQAccessToken getAccessToken(HashMap<String, String> params) {
		/*SinaAccessToken accessToken = new SinaAccessToken();
		String body = ThirdAuthClient.getAccessToken(params);
		try{
			JSONObject jsonO = new JSONObject(body);
			accessToken.setAccessToken(jsonO.getString("access_token"));
			accessToken.setExpiresIn(jsonO.getString("expires_in"));
			accessToken.setUid(jsonO.getString("uid"));			
			return accessToken;
		}catch(JSONException e){
			e.printStackTrace();
		}	
		return null;*/
		
		QQAccessToken accessToken = new QQAccessToken();
		StringBuffer sb = new StringBuffer();
		sb.append(params.get("path")).
		append("?").append("client_id=").append(params.get("client_id")).
		append("&").append("client_secret=").append(params.get("client_secret")).
		append("&").append("grant_type=").append(params.get("grant_type")).
		append("&").append("redirect_uri=").append(params.get("redirect_uri")).
		append("&").append("code=").append(params.get("code")).
		append("&").append("state=").append(params.get("state"));
		
		params.put("path", sb.toString());
		
		String body = ThirdAuthClient.getSSL(params);
		
		String res[] = body.split("&");
		int pos = res[0].indexOf('=');
		accessToken.setAccessToken(res[0].substring(pos+1,res[0].length()));
		pos = res[1].indexOf('=');
		accessToken.setExpiresIn(res[1].substring(pos+1,res[1].length()));
		return accessToken;
		
	}

	@Override
	public String getOpenId(HashMap<String, String> params) {
		StringBuffer sb = new StringBuffer();
		sb.append(params.get("path")).
		append("?").append("access_token=").append(params.get("access_token"));
		
		params.put("path", sb.toString());
		
		String body = ThirdAuthClient.getSSL(params);
		String openid = "";
		
		try{
			int posb = body.indexOf('{');
			int pose = body.indexOf('}');
			body = body.substring(posb,pose+1);
			
			JSONObject jsonO = new JSONObject(body);
			
			openid =  jsonO.getString("openid");
		}catch(JSONException e){
			e.printStackTrace();
		}
		
		
		return openid;
	}

	@Override
	public QQUser getUser(HashMap<String, String> params) {
		QQUser qqUser = new QQUser();	
		StringBuffer sb = new StringBuffer();
		sb.append(params.get("path")).
		append("?").append("access_token=").append(params.get("access_token")).
		append("&").append("oauth_consumer_key=").append(params.get("oauth_consumer_key")).
		append("&").append("openid=").append(params.get("openid"));
		params.put("path", sb.toString());		
		
		String body = ThirdAuthClient.getSSL(params);
		try{		
			JSONObject jsonO = new JSONObject(body);
			qqUser.setNickname(jsonO.getString("nickname"));
			qqUser.setFigureurl_30(jsonO.getString("figureurl"));
			qqUser.setFigureurl_50(jsonO.getString("figureurl_1"));
			qqUser.setFigureurl_100(jsonO.getString("figureurl_2"));
		}catch(JSONException e){
			e.printStackTrace();
		}		
		return qqUser;
	}

}
